import pickle
import tkinter
import tkinter.messagebox

def main():
    # 实例化一个对象，建立window窗口
    window = tkinter.Tk()
    # window窗口标题
    window.title("第一个窗口")
    # window 窗口大小
    window.geometry("400x300")

    # 居中计算
    x = window.winfo_screenwidth()
    y = window.winfo_screenheight()
    x = (x - 400) / 2
    y = (y - 300) / 2

    # 设置位置
    window.geometry("%dx%d+%d+%d" % (400, 300, x, y))

    canvas = tkinter.Canvas(window, width=400, height=135, bg="yellow")
    image_file = tkinter.PhotoImage(file="20.png")
    image = canvas.create_image(200, 0, anchor="n", image=image_file)
    canvas.pack(side="top")

    # Label
    lb_name = tkinter.Label(window, text="User name:", font=("Arial", 14))
    lb_name.place(x=10, y=150)

    lb_pwd = tkinter.Label(window, text="Password:", font=("Arial", 14))
    lb_pwd.place(x=10, y=190)

    # entry
    entry_val_user_name = tkinter.StringVar()
    entry_val_user_name.set("input user name")
    entry_user_name = tkinter.Entry(window, textvariable=entry_val_user_name, font=("Arial", 14))
    entry_user_name.place(x=120, y=150)

    entry_val_pwd = tkinter.StringVar()
    entry_val_pwd.set("input password")
    entry_paw = tkinter.Entry(window, textvariable=entry_val_pwd, font=("Arial", 14))
    entry_paw.place(x=120, y=190)

    def login():
        # 获取输入值
        input_user_name = entry_val_user_name.get()
        input_pwd = entry_val_pwd.get()

        try:
            with open("user.pickle", "rb") as usr_file:
                user_info = pickle.load(usr_file)  # pickle: 将file中的对象序列化读出
        except FileNotFoundError:
            # 不存在，存入
            with open('user.pickle', 'wb') as usr_file:
                user_info = {'admin': 'admin'}
                pickle.dump(user_info, usr_file)
                usr_file.close()  # 必须先关闭，否则pickle.load()会出现EOFError: Ran out of input

        # 账号、密码 校验
        if input_user_name in user_info:
            if input_pwd == user_info[input_user_name]:
                tkinter.messagebox.showinfo("登录提示", message="登录成功！")
            else:
                tkinter.messagebox.showinfo("登录提示", message="登录失败，密码错误！")
        else:
            is_signup = tkinter.messagebox.askyesno("提示", "账号不存在，是否进行注册")
            if is_signup:
                signup()

    # 注册函数
    def signup():
        window_signup = tkinter.Toplevel(window)
        window_signup.geometry("300x200")
        window_signup.title("注册窗口")

        # Lable
        tkinter.Label(window_signup, text="User name:").place(x=10, y=10)
        tkinter.Label(window_signup, text="Password:").place(x=10, y=40)
        tkinter.Label(window_signup, text="Confirm Password:").place(x=10, y=70)

        # Entry
        signup_var_username = tkinter.StringVar()
        signup_var_username.set("input username")
        tkinter.Entry(window_signup, textvariable=signup_var_username).place(x=140, y=10)

        signup_var_pwd = tkinter.StringVar()
        signup_var_pwd.set("")
        tkinter.Entry(window_signup, textvariable=signup_var_pwd, show="*").place(x=140, y=40)

        signup_var_confirm_pwd = tkinter.StringVar()
        signup_var_confirm_pwd.set("")
        tkinter.Entry(window_signup, textvariable=signup_var_confirm_pwd, show="*").place(x=140, y=70)

        def signup_btn_command():
            np = signup_var_username.get()
            npf = signup_var_confirm_pwd.get()
            nn = signup_var_pwd.get()

            # 这里是打开我们记录数据的文件，将注册信息读出
            with open('user.pickle', 'rb') as usr_file:
                exist_usr_info = pickle.load(usr_file)
            # 这里就是判断，如果两次密码输入不一致，则提示Error, Password and confirm password must be the same!
            if np != npf:
                tkinter.messagebox.showerror('Error', 'Password and confirm password must be the same!')

            # 如果用户名已经在我们的数据文件中，则提示Error, The user has already signed up!
            elif nn in exist_usr_info:
                tkinter.messagebox.showerror('Error', 'The user has already signed up!')

            # 最后如果输入无以上错误，则将注册输入的信息记录到文件当中，并提示注册成功Welcome！,You have successfully signed up!，然后销毁窗口。
            else:
                exist_usr_info[nn] = np
                with open('user.pickle', 'wb') as usr_file:
                    pickle.dump(exist_usr_info, usr_file)
                tkinter.messagebox.showinfo('Welcome', 'You have successfully signed up!')
                # 然后销毁窗口。
                window_signup.destroy()

        # 注册按钮
        tkinter.Button(window_signup, text="确定注册", command=signup_btn_command).place(x=120, y=130)

    # button
    tkinter.Button(window, text=" 登 录 ", command=login).place(x=130, y=240)
    tkinter.Button(window, text=" 注 册 ", command=signup).place(x=220, y=240)

    window.mainloop()


main()
